package com.bookstore.service.impl;

import com.bookstore.entity.Book;
import com.bookstore.entity.Order;
import com.bookstore.entity.PageBean;
import com.bookstore.mapper.OrderMapper;
import com.bookstore.service.OrderService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author Cofe
 * @Date 2022/5/2 19:07
 */
@Service
public class OrderServiceImpl implements OrderService {
    @Resource
    private OrderMapper om;

    @Override
    public void insertInto(int bid, String book_name, Double book_balance,
                           int user_uid,String book_pic,int buy_id,double book_membalance,
                           String username,String address,String phone) {
        om.insertOrder(bid,book_name,book_balance,user_uid,book_pic,buy_id,book_membalance,username,address,phone);
    }


    @Override
    public List<Order> findorderbyid(int user_uid) {
        List<Order> findorderbyuid = om.findorderbyuid(user_uid);
        return findorderbyuid;

    }

    @Override
    public PageBean<Order> findourderbyPageAndCondition(int currentPage, int pageSize, String username) {
        //计算开始索引
        int begin = (currentPage - 1) * pageSize;
        // 计算查询条目数
        int size = pageSize;
        List<Order> rows = om.selectByPage(begin,size,username);
        //6. 查询总记录数
        int totalCount = om.selectTotalCountByCondition(username);
        //7. 封装PageBean对象
        PageBean<Order> pageBean = new PageBean<>();
        pageBean.setRows(rows);
        pageBean.setTotalCount(totalCount);
        return pageBean;
    }

    @Override
    public String deleteOrder(int buy_id) {
        Integer integer = om.deleteOrderById(buy_id);
        if(integer>0){
            return "success";
        }else{
            return "系统错误...请稍后重试";
        }

    }

    @Override
    public List<Order> selectOrderByUser(String username) {
        List<Order> orders = om.selectOrderByUser(username);
        return orders;
    }

    @Override
    public String receiptOrder(int buy_id) {
        Integer integer = om.receiptOrder(buy_id);
        if(integer>0){
            return "success";
        }else{
            return "系统错误,请稍后重试...";
        }
    }

    @Override
    public PageBean<Order> selectAllOrder(int currentPage, int pageSize) {
        //计算开始索引
        int begin = (currentPage - 1) * pageSize;
        // 计算查询条目数
        int size = pageSize;
        List<Order> rows = om.selectOrderAll(begin,size);
        System.out.println(rows);
        //6. 查询总记录数
        int totalCount = om.selectTotalOrderAll();
        PageBean<Order> pageBean = new PageBean<>();
        pageBean.setRows(rows);
        pageBean.setTotalCount(totalCount);
        return pageBean;
    }
}
